﻿using System;
using System.Linq;
using MatlabBridge;
using MatlabBridge.IO;

class Program
{
    static void Main(string[] args)
    {
        // -- test .mat file interface --
        MatFile matfile = new MatFile(@"test.mat", MatFileAccess.Read);
        Console.WriteLine(matfile.Count);

        // -- test engine connection --
        MatlabEngine eng = new MatlabEngine();
        eng.CommandWindowVisible = true;
        eng.SetResultBufferSize(256);

        eng.Execute("x = [1 2 3];");
        eng.Execute("disp('Hello');");
        Console.WriteLine(eng.LastResult);
        
        // -- test getting arrays in and out of matlab --
        using (MxArray ar = MxArray.CreateFromString("Hello World!"))
            eng["s"] = ar;
        using (MxArray ar = MxArray.CreateFromScalar(2))
            eng["d"] = ar;
        using (MxArray ar = MxArray.CreateFromBoolean(true))
            eng["b"] = ar;
        using (MxArray ar = eng["x"])
        {
            int[] i = ar.GetSize();
            double[] data = ar.AsVectorDouble();
            
            Console.WriteLine("Dimension: " + string.Join(",", i.Select(x => x.ToString()).ToArray()));
            Console.WriteLine("ClassType: " + ar.ClassType);
            Console.WriteLine("ClassName: " + ar.ClassName);
            Console.WriteLine("Data: " + string.Join(",", data.Select(x => x.ToString()).ToArray()));
        }

        double[] newdata = new double[] { 1, 2, 3 };
        using (MxArray ar = MxArray.CreateMatrix(typeof(double), false, newdata.Length))
        {
            ar.SetDataVector(newdata);
            eng["a"] = ar;
        }

        // -- done --
        Console.ReadLine();
    }
}
